package com.shop2cn.iapi.sellerpromotion.testcase;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.sellerpromotion.parameter.CreatePromotionBean;
import com.shop2cn.iapi.sellerpromotion.parameter.PromotionDetailBean;
import com.shop2cn.iapi.sellerpromotion.parameter.PromotionItem;
import com.shop2cn.iapi.sellerpromotion.service.PromotionDetailCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.ymatou.iapi.trading.operate.parameter.enums.PromotionTypeEnum;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.Shop2cnSellerPromotionCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.mongo.Shop2cnSellerPromotionInfoWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
/**
*满减详情
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_Detail{
	private static PromotionDetailBean detailBean;
	private static PromotionDetailCall detailCall;
	private static Integer mchId;
	private static Shop2cnSellerPromotionInfoWapper Shop2cnSellerPromotionInfoWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("满减详情");
		try {
			Shop2cnSellerPromotionInfoWapper=new Shop2cnSellerPromotionInfoWapper();
			mchId=GodmchManageCallService.CreateSellerMch();
		} catch (Exception e) {
		}
	}

	@Before
	public void caseUp() {
		detailBean=new PromotionDetailBean();
		detailCall=new PromotionDetailCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(detailCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
		try {
			Shop2cnSellerPromotionCallService.EndAllEnablePromotion(mchId);
		} catch (IOException e) {
//			e.printStackTrace();
		}
	}
	private void verifyDB(Integer promotionId, CreatePromotionBean createpromotionBean2) throws Exception {
		Map m = Shop2cnSellerPromotionInfoWapper.getPromotionById(promotionId);
		if (m != null && m.get("sid") != null) {
			//查询详情接口不返还sellerid
//			Logger.verifyEquals(createpromotionBean2.getSellerId(), m.get("sid"), "验证SellerId");
			Logger.verifyEquals(createpromotionBean2.getName(), m.get("name"), "验证name");
			//新增 20200729 是否显示在首页
			Logger.verifyEquals(createpromotionBean2.getShowHomePage(), m.get("shp"), "验证shp");
           //新增 20200810
		   Logger.verifyEquals(createpromotionBean2.getBannerUrl(), m.get("bn"), "验证bn");
		   Logger.verifyEquals(createpromotionBean2.getBgColor(), m.get("bgc"), "验证bgc");
		   Logger.verifyEquals(createpromotionBean2.getTitleColor(), m.get("tc"), "验证tc");
			   
//			Logger.verifyEquals(createpromotionBean2.getPromotionInfo().getPromotionDesc(), m.get("des"), "验证des");
			if (createpromotionBean2.getBegin().compareTo(YMTDateUtil.getBeforeOrNextDay(0, YMTDateUtil.YMDHMS)) > 0) {
				Logger.verifyEquals(createpromotionBean2.getBegin().substring(0, 16),
						YMTDateUtil.getFromDTSDate(m.get("bt").toString(), YMTDateUtil.YMDHMS).substring(0, 16), "验证bt");
			} else {
				Logger.verifyNotNull(YMTDateUtil.getFromDTSDate(m.get("bt").toString(), YMTDateUtil.YMDHMS), "验证bt");
			}

			Logger.verifyEquals(createpromotionBean2.getEnd().substring(0, 16),
					YMTDateUtil.getFromDTSDate(m.get("et").toString(), YMTDateUtil.YMDHMS).substring(0, 16), "验证et");
			Logger.verifyEquals(createpromotionBean2.getSameProductUse(), m.get("issp"), "验证issameproduct");
			Logger.verifyEquals(createpromotionBean2.getType(), m.get("type"), "验证type");
			
			// 1：全场商品 2：指定商品 
//			if (createpromotionBean2.getProductIdList()==null||createpromotionBean2.getProductIdList().size()<=0) {
//				Logger.verifyEquals(1, m.get("pjm"), "验证pjm");
//			} else
//				Logger.verifyEquals(2, m.get("pjm"), "验证pjm");
//			}
			// 未操作
			Logger.verifyEquals(0, m.get("em"), "验证em");
			List<Map> lm = (List<Map>) m.get("vs");
			List<PromotionItem> pvl = createpromotionBean2.getPromotionItemList();
			Logger.verifyEquals(lm.size(), pvl.size(), "验证PromotionValueSize");
			int mytype = 0;
			if (PromotionTypeEnum.HalfIfCountEnough.getCode() == createpromotionBean2.getType() || PromotionTypeEnum.DiscountIfCountEnough.getCode() == createpromotionBean2.getType()
					|| PromotionTypeEnum.ReduceIfCountEnough.getCode() == createpromotionBean2.getType()) {
				// 满件
				Logger.debug("满件活动");
				mytype = 1;
			} else {
				// 满额
				Logger.debug("满额活动");
				mytype = 2;
			}
			// 多个验证
			for (PromotionItem promotionValue : pvl) {
				if (mytype == 1) {
					BigDecimal s = promotionValue.getUseCondition().setScale(2);
					BigDecimal v = promotionValue.getValue();
					for (Map tm : lm) {
						if (s.setScale(2).equals(BigDecimal.valueOf(Double.valueOf(tm.get("rv").toString())).setScale(2))) {
							verifyPromotionValue(createpromotionBean2.getType(), tm, promotionValue);
						}
					}
				} else if (mytype == 2) {
					BigDecimal s = promotionValue.getUseCondition();
					BigDecimal v = promotionValue.getValue();
					for (Map tm : lm) {
						if (s.setScale(2).equals(new BigDecimal(tm.get("rv").toString()).setScale(2))) {
							verifyPromotionValue(createpromotionBean2.getType(), tm, promotionValue);
						}
					}
				}
			}
			List<Map> psm = (List<Map>) m.get("ps");
			if (psm != null) {
				for (Map map : psm) {
					Logger.verifyNotNull(map.get("pidg"), "验证pidg");
					Logger.verifyNotNull(map.get("pidn"), "验证pidn");
					//这个字段目前没用
//					Logger.verifyNotEquals(0,map.get("pidn"), "验证pidn非0");
				}
//				List<String> temppids = createpromotionBean2.getProductIdList();
//				List<String> pids=new ArrayList<>();
//				for (String string : temppids) {
//					if(!pids.contains(string)){
//						pids.add(string);
//					}
//				}
//				Logger.verifyEquals(pids.size(), psm.size(), "验证PromotionValueSize");
//				for (String pid : pids) {
//					Logger.verifyIsContains(pid, psm.toString(), "验证包含pid");
//				}
			}
			}
		}
	private void verifyPromotionValue(int type, Map m, PromotionItem promotionValue) {
		if (type == 1) {
			Logger.verifyEquals(promotionValue.getUseCondition().toString(), m.get("rv").toString(), "验证rv");
			Logger.verifyEquals(promotionValue.getValue(), new BigDecimal(m.get("dv").toString()), "验证dv");

		}
		// 折扣的时候/10
		else if (type == 2) {
			Logger.verifyEquals(promotionValue.getUseCondition().toString(), m.get("rv").toString(), "验证rv");
			Logger.verifyEquals(promotionValue.getValue(), new BigDecimal(m.get("dv").toString()), "验证dv");

		} else if (type == 3) {
			Logger.verifyEquals(promotionValue.getUseCondition(), new BigDecimal(m.get("rv").toString()), "验证rv");
			Logger.verifyEquals(promotionValue.getValue(), new BigDecimal(m.get("dv").toString()), "验证dv");
		} else if (type == 4) {
			Logger.verifyEquals(promotionValue.getUseCondition(), new BigDecimal(m.get("rv").toString()), "验证rv");
			Logger.verifyEquals(promotionValue.getValue(), new BigDecimal(m.get("dv").toString()), "验证dv");

		} else if (type == 5) {
			Logger.verifyEquals("2", m.get("rv").toString(), "验证rv");
			Logger.verifyEquals(new BigDecimal(0.5), new BigDecimal(m.get("dv").toString()), "验证dv");

		}
		//是否包邮 summing 新增20191227
		Logger.verifyEquals(promotionValue.getfreeFreight(), m.get("ff")==null?false:(boolean)m.get("ff"), "验证ff");
		Logger.verifyEquals(promotionValue.getFreeRegions(), m.get("rg")==null?null:m.get("rg"), "验证rg");
		
		Logger.comment("promotionValue.getfreeFreight():"+ m.get("ff"));
		Logger.comment("promotionValue.getFreeRegions():"+ m.get("rg"));
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Detail_001() {
		Logger.start(true,"查询满减详情成功,满额减3");
		try {
			CreatePromotionBean createpromotionBean = Shop2cnSellerPromotionCallService.initCreatePromotionBean(mchId, 3);
			Integer promotionId = Shop2cnSellerPromotionCallService.CreatepromotionCall(createpromotionBean);
			detailBean.setSellerId(mchId);
			detailBean.setPromotionId(promotionId);
			detailCall.setData(detailBean);
			detailCall.callService();			
			Logger.verifyEquals("200", detailCall.getString("code"), "验证返回");
			Logger.debug(FormatUtil.beanToGSONString(detailCall.getData()));
			verifyDB(detailCall.getData().getPromotionId(), detailCall.getData());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Detail_002() {
		Logger.start(true,"查询满减详情成功,满额折4");
		try {
			CreatePromotionBean createpromotionBean = Shop2cnSellerPromotionCallService.initCreatePromotionBean(mchId, 4);
			Integer promotionId = Shop2cnSellerPromotionCallService.CreatepromotionCall(createpromotionBean);
			detailBean.setSellerId(mchId);
			detailBean.setPromotionId(promotionId);
			detailCall.setData(detailBean);
			detailCall.callService();			
			Logger.verifyEquals("200", detailCall.getString("code"), "验证返回");
			Logger.debug(FormatUtil.beanToGSONString(detailCall.getData()));
			verifyDB(detailCall.getData().getPromotionId(), detailCall.getData());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Detail_003() {
		Logger.start(true,"查询满减详情成功,满件减1");
		try {
			CreatePromotionBean createpromotionBean = Shop2cnSellerPromotionCallService.initCreatePromotionBean(mchId, 1);
			Integer promotionId = Shop2cnSellerPromotionCallService.CreatepromotionCall(createpromotionBean);
			detailBean.setSellerId(mchId);
			detailBean.setPromotionId(promotionId);
			detailCall.setData(detailBean);
			detailCall.callService();			
			Logger.verifyEquals("200", detailCall.getString("code"), "验证返回");
			Logger.debug(FormatUtil.beanToGSONString(detailCall.getData()));
			verifyDB(detailCall.getData().getPromotionId(), detailCall.getData());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Detail_004() {
		Logger.start(true,"查询满减详情成功,满件折2");
		try {
			CreatePromotionBean createpromotionBean = Shop2cnSellerPromotionCallService.initCreatePromotionBean(mchId, 2);
			Integer promotionId = Shop2cnSellerPromotionCallService.CreatepromotionCall(createpromotionBean);
			detailBean.setSellerId(mchId);
			detailBean.setPromotionId(promotionId);
			detailCall.setData(detailBean);
			detailCall.callService();			
			Logger.verifyEquals("200", detailCall.getString("code"), "验证返回");
			Logger.debug(FormatUtil.beanToGSONString(detailCall.getData()));
			verifyDB(detailCall.getData().getPromotionId(), detailCall.getData());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Detail_005() {
		Logger.start(true,"查询满减详情成功,外币币种USD");
		try {
			CreatePromotionBean createpromotionBean = Shop2cnSellerPromotionCallService.initCreatePromotionBean(mchId, 3);
			createpromotionBean.setCurrency("USD");
			Integer promotionId = Shop2cnSellerPromotionCallService.CreatepromotionCall(createpromotionBean);
			detailBean.setSellerId(mchId);
			detailBean.setPromotionId(promotionId);
			detailCall.setData(detailBean);
			detailCall.callService();			
			Logger.verifyEquals("200", detailCall.getString("code"), "验证返回");
			Logger.debug(FormatUtil.beanToGSONString(detailCall.getData()));
			verifyDB(detailCall.getData().getPromotionId(), detailCall.getData());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Detail_006() {
		Logger.start(true,"查询满减详情成功,多层级");
		try {
			CreatePromotionBean createpromotionBean = Shop2cnSellerPromotionCallService.initCreatePromotionBean(mchId, 1);
			PromotionItem e=new PromotionItem();
			e.setLevel(1);
			e.setUseCondition(BigDecimal.valueOf(200));
			e.setValue(BigDecimal.valueOf(20));
			createpromotionBean.getPromotionItemList().add(e);
			Integer promotionId = Shop2cnSellerPromotionCallService.CreatepromotionCall(createpromotionBean);
			detailBean.setSellerId(mchId);
			detailBean.setPromotionId(promotionId);
			detailCall.setData(detailBean);
			detailCall.callService();			
			Logger.verifyEquals("200", detailCall.getString("code"), "验证返回");
			Logger.debug(FormatUtil.beanToGSONString(detailCall.getData()));
			verifyDB(detailCall.getData().getPromotionId(), detailCall.getData());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_Detail_007() {
		Logger.start(true,"不输入sellerid失败");
		try {
			detailBean.setPromotionId(1);
			detailCall.setData(detailBean);
			detailCall.callService();			
			Logger.verifyEquals("400", detailCall.getString("code"), "验证返回");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_Detail_008() {
		Logger.start(true,"不输入promotionId失败");
		try {
			detailBean.setSellerId(1);
			detailCall.setData(detailBean);
			detailCall.callService();			
			Logger.verifyEquals("400", detailCall.getString("code"), "验证返回");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_Detail_009() {
		Logger.start(true,"输入sellerid和promotionId不匹配失败");
		try {
			detailBean.setSellerId(1);
			detailBean.setPromotionId(2);
			detailCall.setData(detailBean);
			detailCall.callService();			
			Logger.verifyEquals("200", detailCall.getString("code"), "验证返回");
			Logger.verifyEquals(true, detailCall.getJsonObject().get("data").isJsonNull(), "验证不返还结果");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Detail_010() {
		Logger.start(true,"查询满减详情成功,sameProductUse为false");
		try {
			CreatePromotionBean createpromotionBean = Shop2cnSellerPromotionCallService.initCreatePromotionBean(mchId, 3);
			createpromotionBean.setSameProductUse(false);
			Integer promotionId = Shop2cnSellerPromotionCallService.CreatepromotionCall(createpromotionBean);
			detailBean.setSellerId(mchId);
			detailBean.setPromotionId(promotionId);
			detailCall.setData(detailBean);
			detailCall.callService();			
			Logger.verifyEquals("200", detailCall.getString("code"), "验证返回");
			Logger.debug(FormatUtil.beanToGSONString(detailCall.getData()));
			verifyDB(detailCall.getData().getPromotionId(), detailCall.getData());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Detail_011() {
		Logger.start(true,"查询外币JPY满减详情成功");
		try {
			//创建渠道商，创建日币商品
			int rootUserId = GodmchManageCallService.CreateCountrySellerMch("JPN");
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,"AutoTest",false);
			addAgentProductBean.setCurrency("JPY");
			double price=123;
			addAgentProductBean.getCatalogList().forEach(a->{
				//设置价格
				a.getAgentPriceList().forEach(b->{
					b.setAgentPrice(price);
				});
				a.setDirectPrice(price);
			});
		 	String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
		 	
			CreatePromotionBean createpromotionBean = Shop2cnSellerPromotionCallService.initCreatePromotionBean(rootUserId, 2);
			createpromotionBean.setSameProductUse(true);
			createpromotionBean.setCurrency("JPY");
			createpromotionBean.setProductIdList(Arrays.asList(productId));
			Integer promotionId = Shop2cnSellerPromotionCallService.CreatepromotionCall(createpromotionBean);
			Thread.sleep(1000);
			detailBean.setSellerId(rootUserId);
			detailBean.setPromotionId(promotionId);
			detailCall.setData(detailBean);
			detailCall.callService();			
			Logger.verifyEquals("200", detailCall.getString("code"), "验证返回");
			Logger.debug(FormatUtil.beanToGSONString(detailCall.getData()));
			verifyDB(detailCall.getData().getPromotionId(), detailCall.getData());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Detail_012() {
		Logger.start(true,"查询满减详情成功,满额减3,全部地区包邮");
		try {
			CreatePromotionBean createpromotionBean = Shop2cnSellerPromotionCallService.initCreatePromotionBean(mchId, 3);
			
			createpromotionBean.getPromotionItemList().get(0).setfreeFreight(true);
			
			Integer promotionId = Shop2cnSellerPromotionCallService.CreatepromotionCall(createpromotionBean);
			detailBean.setSellerId(mchId);
			detailBean.setPromotionId(promotionId);
			detailCall.setData(detailBean);
			detailCall.callService();			
			Logger.verifyEquals("200", detailCall.getString("code"), "验证返回");
			Logger.debug(FormatUtil.beanToGSONString(detailCall.getData()));
			verifyDB(detailCall.getData().getPromotionId(), detailCall.getData());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Detail_013() {
		Logger.start(true,"查询满减详情成功,满额减3,一个地区包邮-上海包邮");
		try {
			CreatePromotionBean createpromotionBean = Shop2cnSellerPromotionCallService.initCreatePromotionBean(mchId, 3);
			
			//新增 包邮20191227
			createpromotionBean.getPromotionItemList().get(0).setfreeFreight(true);
			List<String> freeRegions=new ArrayList<>();
			freeRegions.add("上海");
			createpromotionBean.getPromotionItemList().get(0).setFreeRegions(freeRegions);
			
			Integer promotionId = Shop2cnSellerPromotionCallService.CreatepromotionCall(createpromotionBean);
			detailBean.setSellerId(mchId);
			detailBean.setPromotionId(promotionId);
			detailCall.setData(detailBean);
			detailCall.callService();			
			Logger.verifyEquals("200", detailCall.getString("code"), "验证返回");
			Logger.debug(FormatUtil.beanToGSONString(detailCall.getData()));
			verifyDB(detailCall.getData().getPromotionId(), detailCall.getData());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Detail_014() {
		Logger.start(true,"查询满减详情成功,满额减3,两个地区包邮-上海、北京包邮");
		try {
			CreatePromotionBean createpromotionBean = Shop2cnSellerPromotionCallService.initCreatePromotionBean(mchId, 3);
			
			//新增 包邮20191227
			createpromotionBean.getPromotionItemList().get(0).setfreeFreight(true);
			List<String> freeRegions=new ArrayList<>();
			freeRegions.add("上海");
			freeRegions.add("北京");
			createpromotionBean.getPromotionItemList().get(0).setFreeRegions(freeRegions);
			
			Integer promotionId = Shop2cnSellerPromotionCallService.CreatepromotionCall(createpromotionBean);
			detailBean.setSellerId(mchId);
			detailBean.setPromotionId(promotionId);
			detailCall.setData(detailBean);
			detailCall.callService();			
			Logger.verifyEquals("200", detailCall.getString("code"), "验证返回");
			Logger.debug(FormatUtil.beanToGSONString(detailCall.getData()));
			verifyDB(detailCall.getData().getPromotionId(), detailCall.getData());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Detail_015() {
		Logger.start(true,"查询满减详情成功,是否显示在首页ShowHomePage=true");
		try {
			CreatePromotionBean createpromotionBean = Shop2cnSellerPromotionCallService.initCreatePromotionBean(mchId, 3);
			createpromotionBean.setShowHomePage(true);
			Integer promotionId = Shop2cnSellerPromotionCallService.CreatepromotionCall(createpromotionBean);
			detailBean.setSellerId(mchId);
			detailBean.setPromotionId(promotionId);
			detailCall.setData(detailBean);
			detailCall.callService();			
			Logger.verifyEquals("200", detailCall.getString("code"), "验证返回");
			Logger.debug(FormatUtil.beanToGSONString(detailCall.getData()));
			verifyDB(detailCall.getData().getPromotionId(), detailCall.getData());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Detail_016() {
		Logger.start(true,"查询满减详情成功,新增banner url、背景颜色、标题颜色 20200810");
		try {
			CreatePromotionBean createpromotionBean = Shop2cnSellerPromotionCallService.initCreatePromotionBean(mchId, 3);
		   	createpromotionBean.setBannerUrl("落地页banner_"+TestDataManager.getRandomIntNum(5));
			createpromotionBean.setBgColor("背景颜色_"+TestDataManager.getRandomIntNum(5));
			createpromotionBean.setTitleColor("标题颜色_"+TestDataManager.getRandomIntNum(5));
			
			Integer promotionId = Shop2cnSellerPromotionCallService.CreatepromotionCall(createpromotionBean);
			detailBean.setSellerId(mchId);
			detailBean.setPromotionId(promotionId);
			detailCall.setData(detailBean);
			detailCall.callService();			
			Logger.verifyEquals("200", detailCall.getString("code"), "验证返回");
			Logger.debug(FormatUtil.beanToGSONString(detailCall.getData()));
			verifyDB(detailCall.getData().getPromotionId(), detailCall.getData());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}